<?php

namespace app\rent\controller;

use think\Controller;

class Product extends Controller
{
    protected $BusinessModel;
    protected $CategoryModel;
    protected $ProductModel;
    protected $CollectionModel;
    public function _initialize()
    {
        // 继承父类
        parent::_initialize();
        $this->BusinessModel = model('Business.Business');
        $this->CategoryModel = model('Category');
        $this->ProductModel = model('Product.Product');
        $this->CollectionModel = model('Business.Collection');
    }

    // 首页
    public function home()
    {
        $suggestlist = $this->CategoryModel->where('flag', 'recommend')->order('createtime', 'DESC')->limit(5)->select();

        $productlist = $this->ProductModel->where(['status' => '1', 'rentstatus' => ['in', ['2', '3']]])->order('createtime', 'DESC')->limit(5)->select();

        $caselist = $this->CategoryModel->where(['flag' => ['in', ['index', 'hot']]])->order('createtime', 'DESC')->limit(8)->select();

        $data = [
            'suggestlist' => $suggestlist,
            'productlist' => $productlist,
            'caselist' => $caselist,
        ];

        $this->success('', '', $data);
    }
    // 文章列表
    public function category()
    {
        $page = $this->request->param('page', 1, 'trim');

        $catelist = $this->CategoryModel->where('flag', 'recommend')->order('createtime', 'DESC')->limit(5)->select();

        $list = $this->CategoryModel->page($page, 5)->select();

        $data = [
            'catelist' => $catelist,
            'list' => $list
        ];

        if (count($list) > 0) {
            $this->success('', '', $data);
        } else {
            $this->error('暂无数据');
        }
    }
    // 文章详情
    public function categoryinfo()
    {
        $cateid = $this->request->param('cateid', 0, 'trim');
        $busid = $this->request->param('busid', 0, 'trim');
        $info = $this->CategoryModel->find($cateid);
        $prev = $this->CategoryModel->where('id', 'lt', $cateid)->order('id', 'DESC')->find();
        $next = $this->CategoryModel->where('id', 'gt', $cateid)->order('id', 'ASC')->find();
        $collect_statsu = false;
        $collectinfo = $this->CollectionModel->Where(['busid' => $busid, 'cateid' => $cateid])->find();
        if ($collectinfo) {
            $collect_statsu = true;
        }
        $info['collect_statsu'] = $collect_statsu;
        $data = [
            'info' => $info,
            'prev' => $prev,
            'next' => $next,
        ];
        $this->success('', '', $data);
    }

    // 文章收藏功能
    public function categorycollection()
    {
        $cateid = $this->request->param('cateid', 0, 'trim');
        $busid = $this->request->param('busid', 0, 'trim');

        $cateinfo = $this->CategoryModel->find($cateid);

        if (!$cateinfo) {
            $this->error('文章不存在');
        }

        $businessinfo = $this->BusinessModel->find($busid);
        if (!$businessinfo) {
            $this->error('用户不存在');
        }

        $collectinfo = $this->CollectionModel->Where(['busid' => $busid, 'cateid' => $cateid])->find();

        // 取消收藏 
        if ($collectinfo) {
            $result = $this->CollectionModel->destroy($collectinfo['id']);
            if ($result) {
                $this->success('取消收藏成功');
            } else {
                $this->error($this->CollectionModel->getError());
            }
        }
        // 收藏 
        else {
            $data = [
                'busid' => $busid,
                'cateid' => $cateid,
            ];
            $result = $this->CollectionModel->validate('common/Business/Collection.cate')->save($data);

            if ($result) {
                $this->success('收藏成功');
            } else {
                $this->error($this->CollectionModel->getError());
            }
        }
    }
    // 租赁商品列表
    public function rental()
    {
        $page = $this->request->param('page', 1, 'trim');

        $rentallist = $this->ProductModel->where(['status' => '1', 'rentstatus' => ['in', ['2', '3']], 'flag' => '1'])->limit(5)->select();

        $list = $this->ProductModel->where(['status' => '1', 'rentstatus' => ['in', ['2', '3']]])->page($page, 5)->select();

        $data = [
            'rentallist' => $rentallist,
            'list' => $list
        ];

        if (count($list) > 0) {
            $this->success('', '', $data);
        } else {
            $this->error('暂无数据');
        }
    }
    // 全部租赁商品列表
    public function list(){
        $keywords = $this->request->param('keywords', '', 'trim');
        $where=['status' => '1', 'rentstatus' => ['in', ['2', '3']]];
        if($keywords){
            $where['name']=['like',"%$keywords%"];
        }
        $rentallist = $this->ProductModel->where($where)->order('createtime','desc')->select();
        if($rentallist){
            $this->success('', '', $rentallist);
        }else{
            $this->error('暂无数据');
        }
    }
    // 商品详情
    public function proinfo()
    {
        $proid = $this->request->param('proid', 0, 'trim');
        $busid = $this->request->param('busid', 0, 'trim');
        $info = $this->ProductModel->find($proid);
        $collectinfo = $this->CollectionModel->Where(['busid' => $busid, 'proid' => $proid])->find();
        $collect_statsu=false;
        if ($collectinfo) {
            $collect_statsu = true;
        }
        $info['collect_statsu'] = $collect_statsu;
        $this->success('', '', $info);
    }
     // 商品收藏功能
     public function procollection()
     {
         $proid = $this->request->param('proid', 0, 'trim');
         $busid = $this->request->param('busid', 0, 'trim');
 
         $productinfo = $this->ProductModel->find($proid);
 
         if (!$productinfo) {
             $this->error('商品不存在');
         }
 
         $businessinfo = $this->BusinessModel->find($busid);
         if (!$businessinfo) {
             $this->error('用户不存在');
         }
 
         $collectinfo = $this->CollectionModel->Where(['busid' => $busid, 'proid' => $proid])->find();
 
         // 取消收藏 
         if ($collectinfo) {
             $result = $this->CollectionModel->destroy($collectinfo['id']);
             if ($result) {
                 $this->success('取消收藏成功');
             } else {
                 $this->error($this->CollectionModel->getError());
             }
         }
         // 收藏 
         else {
             $data = [
                 'busid' => $busid,
                 'proid' => $proid,
             ];
             $result = $this->CollectionModel->validate('common/Business/Collection.pro')->save($data);
 
             if ($result) {
                 $this->success('收藏成功');
             } else {
                 $this->error($this->CollectionModel->getError());
             }
         }
     }
}
